package com.project.config;

import javax.sql.DataSource;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.project.interceptor.CORSInterceptor;
import com.project.interceptor.LoginInterceptor;
import com.project.util.SpringUtil;

@Configuration
public class RestServletContextConfig extends WebMvcConfigurerAdapter {
	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        /*registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE");*/
    }
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new LoginInterceptor())
        .addPathPatterns("/**")
        .excludePathPatterns("/**/configuration/**")
        .excludePathPatterns("/**/error/**")
        .excludePathPatterns("/**/swagger**")
        .excludePathPatterns("/**/api-docs/**")
        .excludePathPatterns("/**/login/**")
        .excludePathPatterns("/**/article/category/**")
        .excludePathPatterns("/**/article/ops/**")
        .excludePathPatterns("/**/article/articlePhotoList/**")
        .excludePathPatterns("/**/article/articleList/**")
        .excludePathPatterns("/**/article/list/**")
        .excludePathPatterns("/**/activity/list/**")
        .excludePathPatterns("/**/activity/ops/**")
        .excludePathPatterns("/**/weixin/config/**")
        .excludePathPatterns("/**/weixin/jump/**");
        
        
        registry.addInterceptor(new CORSInterceptor())
        .addPathPatterns("/**");
        super.addInterceptors(registry);
    }
	
	@Bean
    public SpringUtil springUtil(){
		return new SpringUtil();
	}
	
	@Bean
	public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
		org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
		config.setMapUnderscoreToCamelCase(true);
		config.setLogImpl(StdOutImpl.class);

		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setConfiguration(config);

		return sqlSessionFactoryBean.getObject();

	}

	@Bean
	public DataSourceTransactionManager qinquUserDataSourceTransactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

}
